/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.eit.ntc.lmsbackend.struts.action;


import com.eit.ntc.lmsbackend.struts.form.LMSForm;
import com.eit.ntc.struts.action.AbstractWebFlowAction;
import com.eit.toschoolonline.lms.jdo.LessonM;
import com.eit.toschoolonline.lms.jdo.SubjectM;
import com.eit.toschoolonline.lms.service.LearningItemService;
import com.eit.toschoolonline.lms.service.LessonService;
import com.eit.toschoolonline.lms.service.SubjectService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.validation.Errors;
import org.springframework.webflow.execution.Event;
import org.springframework.webflow.execution.RequestContext;

/**
 *
 * @author chanon
 */
public class LessonAction extends AbstractWebFlowAction{
    private LessonService lessonService;
    private SubjectService subjectService;
    private LearningItemService itemService;
     public Event MainLesson(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
          lForm.setSubjectM(null);
          lForm.getLessonList().clear();
          lForm.getSubjectList().clear();
           lForm.setSelectSubject("");
           lForm.setErrorMode("");
          SubjectService sservice = getSubjectService();
          lForm.setSubjectList(sservice.getSubjectAll());
              
          return new Event(this, "success");
        }
    public Event AddLesson(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
              lForm.setErrorMode("");
              lForm.setEditMode("");
              lForm.setLessonCredit("");
              lForm.setLessonCode("");
              lForm.setLessonDescription(null);   
              lForm.setLessonName("");
             return new Event(this, "success");
        } 
    public Event GetListLesson(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
              lForm.setErrorMode("");
              lForm.setEditMode("");
              List lessonList = new ArrayList();
              SubjectService sservice = getSubjectService();
              String sCode = lForm.getSelectSubject();
              SubjectM subject = null;
              if (sCode!=null){
                 subject = sservice.getSubjectByCode(sCode);
              };
              if (subject==null){
                  if (lForm.getSubjectList().size()>0){
                    subject = (SubjectM) lForm.getSubjectList().get(0);
                  };
              };
              if (subject!=null){
                  lForm.setSubjectM(subject);
                  LessonService lservice = getLessonService();
                  lessonList = lservice.getOrderLessonBySubject(subject.getSubjectCode());

              };
              
              lForm.setLessonList(lessonList);
            
              return new Event(this, "success");
        } 
        
     public Event DelLesson(RequestContext context) throws Exception {
                LMSForm lForm = (LMSForm) getFormObject(context);
                LessonService service = getLessonService();
                LessonM l = service.getLesson(Integer.parseInt(lForm.getInputID()));
                List items = itemService.loadLearningItemByLessonCode(l.getLessonCode());
                if (items!=null && items.size()==0){
                    l.setStatus(LessonM.DELETE_STATUS);    
                    service.updateLesson(l);
                } else {
                    Errors errors = getFormErrors(context);
                    errors.reject("errors.ItemFound");
                    return error();
                }
                
             return success();
        } 
      public Event EditLesson(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
          String LessonID = getHttpServletRequestParameter(context,"id");
          LessonService service = getLessonService();
           LessonM lessonM = null;
             lessonM = service.getLesson(Integer.parseInt(LessonID)); 
              lForm.setErrorMode("");
              lForm.setEditMode("");    
              
              lForm.setLessonCode(lessonM.getLessonCode());
              lForm.setLessonName(lessonM.getLessonName());       
              lForm.setLessonDescription(lessonM.getLessonDesc());
              lForm.setLessonM(lessonM);
             return new Event(this, "success");
        }
       public Event SaveLesson(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
           LessonService service = getLessonService();
           LessonM l = new LessonM();     
           lForm.setErrorMode("");
           if(service.getEnableLessonByCode(lForm.getLessonCode())==null){ 
               l.setLessonCode(lForm.getLessonCode());
               l.setLessonName(lForm.getLessonName());
               l.setLessonDesc(lForm.getLessonDescription());
               SubjectService sservice = getSubjectService();
               SubjectM s = sservice.getSubjectByCode(lForm.getSelectSubject());
               lForm.setSubjectM(s);
               l.setSubject(s);
               l.setStatus(LessonM.ENABLE_STATUS);
               service.createLesson(l);
                return new Event(this, "success");}
           else{
              lForm.setErrorMode("มี LessonCode นี้อยู่แล้วกรุณาตรวจสอบใหม่อีกครั้ง");
               return new Event(this, "fail");
           }
        } 
       public Event SaveEditLesson(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
           LessonService service = getLessonService();
           LessonM l = lForm.getLessonM();      
           lForm.setErrorMode("");
           l.setLessonName(lForm.getLessonName());
          // l.setLessonCode(lForm.getLessonCode());
           l.setLessonDesc(lForm.getLessonDescription());
           l.setSubject(lForm.getSubjectM());
           l.setStatus("ENABLE");
           service.updateLesson(l);
             return new Event(this, "success");
        }



        public Event CancelLesson(RequestContext context) throws Exception {
         LMSForm lForm = (LMSForm) getFormObject(context);
                lForm.setLessonM(null);
             return new Event(this, "success");
        }

    public LessonService getLessonService() {
        return lessonService;
    }

    public void setLessonService(LessonService lessonService) {
        this.lessonService = lessonService;
    }

    public SubjectService getSubjectService() {
        return subjectService;
    }

    public void setSubjectService(SubjectService subjectService) {
        this.subjectService = subjectService;
    }

    public LearningItemService getItemService() {
        return itemService;
    }

    public void setItemService(LearningItemService itemService) {
        this.itemService = itemService;
    }
    

}
